package com.g13.virtualpethospital.service.Implement;

import com.g13.virtualpethospital.entity.CaseEntity;
import com.g13.virtualpethospital.entity.QuestionEntity;
import com.g13.virtualpethospital.exceptions.CaseException;
import com.g13.virtualpethospital.exceptions.QuestionException;
import com.g13.virtualpethospital.service.CaseService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title: CaseServiceImpl
 * @Author yyj1205
 * @Package com.g13.virtualpethospital.service.Implement
 * @Date 24/3/2024 23:11
 * @description: CaseService的一个实现
 */
@Service
public class CaseServiceImpl implements CaseService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public List<CaseEntity> findAllCase() {
        return mongoTemplate.findAll(CaseEntity.class);
    }

    @Override
    public List<CaseEntity> findCaseByType(String type, String subType) {
        Query query = null;
        if((type == "" || type == null) && (subType == "" || subType == null)){
            return findAllCase();
        } else if (type == "" || type == null) {
            query = new Query(Criteria.where("subMenu").is(subType));
        }
        else if (subType == "" || subType == null){
            query = new Query(Criteria.where("menu").is(type));
        }
        else {
            query = new Query(Criteria.where("menu").is(type).and("subMenu").is(subType));
        }
        return mongoTemplate.find(query, CaseEntity.class);
    }

    @Override
    public List<CaseEntity> findCaseByName(String name) {
        if(name == "" || name == null) return findAllCase();
        Query query = new Query(Criteria.where("caseName").is(name));
        return mongoTemplate.find(query, CaseEntity.class);

    }

    @Override
    public List<CaseEntity> findCaseById(String id) {
        return mongoTemplate.find(new Query(Criteria.where("_id").is(id)), CaseEntity.class);
    }

    @Override
    public void addCase(CaseEntity caseEntity) {
        caseEntity.setCaseId(null);
        mongoTemplate.insert(caseEntity);
    }

    @Override
    public void updateCase(CaseEntity caseEntity, String caseId) throws CaseException {
        CaseEntity question1 = mongoTemplate.findById(caseId, CaseEntity.class);
        if(question1 == null) throw new CaseException(CaseException.CaseErrorCode.FIND_NO_CASE);
        Query query = new Query(Criteria.where("_id").is(caseId));
        Update update = new Update();
        if(caseEntity.getCaseCheck() != null)
            update.set("caseCheck", caseEntity.getCaseCheck());
        if(caseEntity.getCaseDiag() != null)
            update.set("caseDiag", caseEntity.getCaseDiag());
        if(caseEntity.getCaseName() != null)
            update.set("caseName", caseEntity.getCaseName());
        if (caseEntity.getCaseTake() != null)
            update.set("caseTake", caseEntity.getCaseTake());
        if (caseEntity.getCaseTherapy() != null)
            update.set("caseTherapy", caseEntity.getCaseTherapy());
        if (caseEntity.getMenu() != null)
            update.set("menu", caseEntity.getMenu());
        if (caseEntity.getSubMenu() != null)
            update.set("subMenu", caseEntity.getSubMenu());
        UpdateResult result = mongoTemplate.upsert(query,update, CaseEntity.class);
    }

    @Override
    public void deleteCase(String caseId) throws CaseException {
        CaseEntity caseEntity = mongoTemplate.findById(caseId, CaseEntity.class);
        if(caseEntity == null) throw new CaseException(CaseException.CaseErrorCode.FIND_NO_CASE);
        DeleteResult result = mongoTemplate.remove(new Query(Criteria.where("_id").is(caseId)), CaseEntity.class);
        if (result.getDeletedCount() == 0) throw new CaseException(CaseException.CaseErrorCode.DEL_FAIL);
    }
}
